package com.ecar.api.support.util;

import java.net.URL;
import java.util.ArrayList;  
import java.util.HashMap;
import java.util.List;  
import java.util.Map;
  
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;  
import org.slf4j.LoggerFactory;  

import com.ecar.api.support.dto.GaodeLocation;
import com.ecar.api.support.dto.Geocodes;
import com.ecar.common.util.HttpClientUtil;
import com.ecar.common.util.HttpUtils;
import com.ecar.common.util.JSONUtil;
  
/** 
 * 高德地图测试 
 *  
 * @author fire 
 * 
 */  
public class GaoDeMapUtil {  
    private static final Logger logger = LoggerFactory.getLogger(GaoDeMapUtil.class);  
  
    // 高德应用的key
    private static String gaodeAppID = Props.getValue("map.gaode.appid");
    
    // 地理编码地址  
    private static String map_codeurl = "http://restapi.amap.com/v3/distance";  
    
//    http://restapi.amap.com/v3/distance?origins=116.481028,39.989643|114.481028,39.989643|115.481028,39.989643&destination=114.465302,40.004717
//    	&output=xml&key=<用户的key>
    
    /** 
     * 输入地址返回识别后的信息 
     *  
     * @param address 
     * @return 返回的类gaodelocation，详见类 
     */  
    private GaodeLocation getLocatoin(String origins,String destination) {  
        GaodeLocation location = null;  
        if (origins != null && destination != null) {  
            try {  
                location = new GaodeLocation();  
                
                String url = map_codeurl.replace("parameters", "");
                String [] a=origins.split(",");
                double b1=0.0;
                double b2=0.0;
                
                b1=Double.parseDouble(a[0]);
                b2=Double.parseDouble(a[1]);
                if(b1>b2){
                	origins=b1+","+b2;
                }else{
                	origins=b2+","+b1;
                }
                
                String [] c=destination.split(",");
                b1=Double.parseDouble(c[0]);
                b2=Double.parseDouble(c[1]);
                if(b1>b2){
                	destination=b1+","+b2;
                }else{
                	destination=b2+","+b1;
                }
                
                //String params = "origins=" + origins + "&destination=" + destination + "&output=json&strategy=2&key=" + gaodeAppID;  
                String params = "origins=" + origins + "&destination=" + destination + "&output=json&type=0&key=" + gaodeAppID;  
                String result = HttpClientUtil.httpPostContent(map_codeurl, params);
                JSONObject jsonObject = JSONObject.fromObject(result);  
  
                // 解析json  
                location.setStatus(jsonObject.getString("status"));  
                location.setInfo(jsonObject.getString("info"));  
                location.setInfocode(jsonObject.getString("infocode"));
                List<Geocodes> geocodes = new ArrayList<>();  
                JSONArray jsonArray = jsonObject.getJSONArray("results");  
                // 遍历解析出来的结果  
                if ((jsonArray != null) && (jsonArray.size() > 0)) { 
                	for(int i=0;i<jsonArray.size();i++){ 
                    JSONObject jo = (JSONObject) jsonArray.get(i);  
                    Geocodes go = new Geocodes();  
                    go.setOrigin_id(jo.getString("origin_id"));  
                    go.setDest_id(jo.getString("dest_id"));
                    go.setDistance(jo.getString("distance"));  
                    go.setDuration(jo.getString("duration"));  
                    /*go.setInfo(jo.getString("info"));
                    go.setCode(jo.getString("code"));  */
                    geocodes.add(go);  
                }  
                }
                location.setGeocodes(geocodes);  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        }  
        return location;  
    }  
    
    
    public static double getDistance(String origins,String destination){
    	GaoDeMapUtil gaoDeMapUtil=new GaoDeMapUtil();
    	
    	if(gaoDeMapUtil.getLocatoin(origins, destination).getStatus().equals("1")){
			String a=gaoDeMapUtil.getLocatoin(origins, destination).getGeocodes().get(0).getDistance();
			return Double.valueOf(a);
		}else{
			return -1;
		}
    }
}  